package com.hundsun.yr.universal.p2p.presenter.impl;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;

import com.hundsun.mobile.zwgt.R;
import com.hundsun.yr.universal.library.data.HSSharedPreferences;
import com.hundsun.yr.universal.library.netstatus.NetUtils;
import com.hundsun.yr.universal.p2p.bean.AdSlideBeen;
import com.hundsun.yr.universal.p2p.bean.BankStatementBeen;
import com.hundsun.yr.universal.p2p.bean.ResponseBean;
import com.hundsun.yr.universal.p2p.bean.UserInfo;
import com.hundsun.yr.universal.p2p.config.Config;
import com.hundsun.yr.universal.p2p.config.Constants;
import com.hundsun.yr.universal.p2p.config.TaskConstants;
import com.hundsun.yr.universal.p2p.interactor.base.RequestInteractor;
import com.hundsun.yr.universal.p2p.interactor.impl.StringRequestGetInteractorImpl;
import com.hundsun.yr.universal.p2p.listener.BaseMultiLoadedListener;
import com.hundsun.yr.universal.p2p.presenter.base.RequestPresenter;
import com.hundsun.yr.universal.p2p.ui.activity.BankRecordActivity;
import com.hundsun.yr.universal.p2p.utils.AndroidDes3Util;
import com.hundsun.yr.universal.p2p.utils.GsonTool;
import com.hundsun.yr.universal.p2p.view.base.BaseListView;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;


/**
 * @version V1.0
 * @Title: BankRecordPresenterImpl.java
 * @Description: TODO(用一句话描述该文件做什么)
 * @date 2015-10-27 上午11:18:59
 * @author:hejun.shen
 * @email:shenhj15971@hundsun.com
 * @replace author:
 * @replace date:
 */
public class BankRecordPresenterImpl implements RequestPresenter, BaseMultiLoadedListener<String>, OnClickListener {

    private Context mContext = null;
    private BaseListView mBaseView = null;
    private RequestInteractor mInteractor = null;
    ResponseBean responseBean;
    /**
     * list的key
     **/
    private String mKey;
    /**
     * 任务名
     **/
    private String mTask_name;
    /**
     * 请求地址
     **/
    private String mUrl;
    /**
     * 加载事件
     **/
    private int mEvent_tag;
    /**
     * 参数
     **/
    private HashMap<String, String> mParams;
    /**
     * 是否刷新
     **/
    private boolean mIsSwipeRefresh;
    /**
     * 用户信息sp文件
     **/
    private HSSharedPreferences sharedPreferences = null;

    public BankRecordPresenterImpl(Context context, BaseListView baseView, String key, ResponseBean bean) {
        // TODO Auto-generated constructor stub
        if (null == baseView) {
            throw new IllegalArgumentException("Constructor's parameters must not be Null");
        }
        this.mContext = context;
        this.mBaseView = baseView;
        this.mInteractor = new StringRequestGetInteractorImpl(this);
        this.mKey = key;
        this.responseBean = bean;
        this.sharedPreferences = new HSSharedPreferences(context, Config.SPPath.file_userLoginedInfo);
    }

    @Override
    public void onSuccess(String task_name, int event_tag, String data) {
        mBaseView.hideLoading();
        mBaseView.closeProgress();
        AdSlideBeen adSlideBeen = (AdSlideBeen) GsonTool.getInstanceByJson(AdSlideBeen.class, data);
        switch (task_name) {
            case TaskConstants.TASK_BILL://银行流水
                switch (adSlideBeen.code) {
                    case Constants.REQUESTCODE_000000:
                        try {
                            String decode = AndroidDes3Util.decode(adSlideBeen.data, sharedPreferences.getShareProf(UserInfo.TOKENID).substring(0, 24), (Date.parse(adSlideBeen.timestamp) + "").substring(0, 8));
                            String[] split = decode.split("\\n");
                            ArrayList<BankStatementBeen> invests = GsonTool.jsonToArrayList(split[1], BankStatementBeen.class);
                            //对数据进行解析
                            if (event_tag == Constants.EVENT_REFRESH_DATA) {
                                //event_tag为刷新界面
                                if (invests.size() > 0) {
                                    mBaseView.refreshListData(invests);
                                } else {
                                    mBaseView.refreshListData(null);
                                    //没有数据显示空页面
                                    mBaseView.showEmpty(0, mContext.getString(R.string.empty_msg), this);
                                }
                            }
                        } catch (Exception e) {
                            e.printStackTrace();

                        }
                        break;

                    default:
                        mBaseView.onFailure(TaskConstants.TASK_BILL);
                        mBaseView.showEmpty(0, mContext.getString(R.string.empty_msg), this);
                        break;
                }
                break;
        }
    }

    @Override
    public void onException(String task_name, String msg) {
        // TODO Auto-generated method stub
        mBaseView.hideLoading();
        mBaseView.closeProgress();

        if (TextUtils.equals(msg, "参数错误")) {
            mBaseView.showError("验证码有误", new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    ((BankRecordActivity) mContext).validate_dialog.show();
                }
            });
        } else {
            mBaseView.showError(msg, this);
        }
        mBaseView.onFailure(task_name);
//		400验证码错误
    }


    /**
     * header|params
     *
     * @param task_name      任务名
     * @param url            请求地址
     * @param event_tag      加载事件{@link Constants}}
     * @param params         参数
     * @param isSwipeRefresh 是否刷新
     */
    @Override
    public void loadingData(String task_name, String url, int event_tag,
                            HashMap<String, String> params, boolean isSwipeRefresh) {
        // TODO Auto-generated method stub

        if (event_tag == Constants.EVENT_REFRESH_DATA) {
            this.mTask_name = task_name;
            this.mUrl = url;
            this.mParams = params;
            this.mIsSwipeRefresh = isSwipeRefresh;
            this.mEvent_tag = event_tag;
        }

        if (event_tag == Constants.EVENT_PROGRESS) {
            mBaseView.showProgress("");
        }
        if (isSwipeRefresh) {
            mBaseView.showLoading(mContext.getString(R.string.common_loading_message));
        }
        if (NetUtils.isNetworkConnected(mContext)) {
            mInteractor.getRequestData(task_name, url, event_tag, params);
        } else {
            mBaseView.hideLoading();
            mBaseView.onFailure(task_name);
            mBaseView.showNetError(this);
        }
    }

    @Override
    public void loadingData(String task_name, String url, int event_tag, HashMap<String, String> params, HashMap<String, String> header, boolean isSwipeRefresh) {

    }

    @Override
    public void loadingData(String task_name, String url, int event_tag, JSONObject params, HashMap<String, String> header, boolean isSwipeRefresh) {

    }

    @Override
    public void initialized() {
        // TODO Auto-generated method stub

    }

    @Override
    public void onClick(View v) {
        loadingData(mTask_name, mUrl, mEvent_tag, mParams, mIsSwipeRefresh);
    }

}
